package service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountWordService {

    private HashMap<String,Integer> wrodFrequency;

    private String filePath;

    public CountWordService( ){
        this.wrodFrequency = new HashMap<String,Integer>();
        this.filePath = "";
    }

    public CountWordService(String filePath){
        this();
        this.filePath  = filePath;
    }

    // 汉字计数
    public void countWord(ArrayList<String> list) {
        list.forEach(word->{
            if (this.wrodFrequency.containsKey(word)) {
                this.wrodFrequency.put(word,this.wrodFrequency.get(word)+1);
            }
            else {
                this.wrodFrequency.put(word,1);
            }
        });
    }

    // 获取文件中所有汉字并返回汉字list
    public ArrayList<String> getChineseCharacters(String file) {
        ArrayList<String> result = new ArrayList<>();
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(file);

        while(m.find()){
            result.add(m.group(0)+"");
        }
        return result;
    }


    public HashMap<String, Integer> getWrodFrequency() {
        return wrodFrequency;
    }

    public void setWrodFrequency(HashMap<String, Integer> wrodFrequency) {
        this.wrodFrequency = wrodFrequency;
    }


    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

}
